/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_DBH_READER_HPP_
#define _DAL_FASTDDS_DBH_READER_HPP_

#include <unordered_map>
#include <mutex>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include "dal_dmabufheap/DmaBufHeapManager.hpp"
#include "dal_fastdds/listener_impl/DataReaderListenerImpl.hpp"
#include "dal_fastdds/utils/Uuid.hpp"
#include "dal_builtin/msg/dal_dma_buf_heap_send_notify_msg.hpp"
#include "dalidl_typesupport_fastrtps_cpp/message_type_support.h"
#include "dal_fastrtps_cpp/type_support_common.hpp"

using namespace eprosima::fastdds::dds;
using namespace dal_dmabufheap;

class DalDbhReader
{
public:
    DalDbhReader();
    virtual ~DalDbhReader();

    void SetFastddsSubscriber(Subscriber*& subscriber);
    void SetFastddsDomainParticipant(DomainParticipant*& participant);

    // used for dmabufheap transfer
    void SetDbhSendNotifyReaderListener(DataReaderListenerImpl* listener);
    void SetDbhSendNotifyReaderQos(const DataReaderQos& qos);
    void SetDbhSendNotifyReaderStatusMask(const StatusMask& mask);
    void SetTypeSupport(const void* ts);
    void SetOriginTopicName(const std::string& origin_topic_name);

    bool CreateDbhBuiltinEntities();
    DataReader* GetDbhSendNotifyReader();

    bool RecvDbhSendNotifyMsg(eprosima::fastdds::dds::SampleInfoSeq& info_seq);
    void ParseDbhSendNotifyMsg();
    bool GetMsgFromDbhBlock(void* msg);

    DmaBufHeapManager::Block* GetDbhBlockFromMap(int32_t global_fd);
    bool DeleteDbhBlockFromMap(int32_t global_fd);

    virtual bool SendDbhRecvNotifyMsg() = 0;
    virtual void SetDbhRecvNotifyMsg() = 0;

protected:
    bool CreateDbhTopic();
    bool CreateDbhInfoReader();
    virtual bool CreateDbhSendNotifyReader() = 0;
    virtual bool CreateDbhRecvNotifyWriter() = 0;

protected:
    std::string origin_topic_name_;
    Subscriber* fastdds_sub_ = nullptr;
    DomainParticipant* fastdds_participant_ = nullptr;
    const message_type_support_callbacks_t* msg_ts_callbacks_ = nullptr;
    MessageTypeSupport_cpp* msg_ts_ = nullptr;

    // used for dmabufheap transfer
    DataReader* dbh_send_notify_reader_ = nullptr;
    Topic* dbh_send_notify_topic_ = nullptr;
    std::string dbh_send_notify_topic_name_;
    DataReaderListenerImpl* dbh_send_notify_reader_listener_ = nullptr;
    DataReaderQos dbh_send_notify_reader_qos_;
    StatusMask dbh_send_notify_reader_status_mask_;
    dal_builtin::msg::DalDmaBufHeapSendNotifyMsg dbh_send_notify_msg_;
    const message_type_support_callbacks_t* dbh_send_notify_msg_ts_callbacks_ = nullptr;

    DataWriter* dbh_recv_notify_writer_ = nullptr;
    Topic* dbh_recv_notify_topic_ = nullptr;
    Publisher* dbh_recv_notify_publisher_ = nullptr;
    std::string dbh_recv_notify_topic_name_;
    const message_type_support_callbacks_t* dbh_recv_notify_msg_ts_callbacks_ = nullptr;

    DataReader* dbh_info_reader_ = nullptr;
    Topic* dbh_info_topic_ = nullptr;
    std::string dbh_info_topic_name_;
    const message_type_support_callbacks_t* dbh_info_ts_callbacks_ = nullptr;

    int32_t dbh_send_notify_global_fd = -1;
    uint32_t dbh_send_notify_offset = 0;
    uint32_t dbh_send_notify_mmap_size = 0;
    uint32_t dbh_send_notify_msg_size = 0;
    uint32_t dbh_send_notify_index = 0;
    uint8_t dbh_send_notify_uuid[UUID_LEN] = {0};

    std::unordered_map<int32_t, DmaBufHeapManager::Block*> block_map_;  // key=global_fd
    std::mutex block_map_mutex_;

    bool is_debug_print_enabled_ = false;
};

#endif  // _DAL_FASTDDS_DBH_READER_HPP_